$(function (){
    var queryString = window.location.search;
    var params = new URLSearchParams(queryString);
    var id=params.get("articleId");
    var username=params.get("username")
    $.ajax({
        url:"/blogSystem/detail",
        data:{
            "articleId":id
        },
        dataType:"json",
        success:function (json){
            console.log(JSON.stringify(json))
            $("#author").html(username)
            $("#title").html(json.title)
            $("#content").html(json.content)
            $("#dateTime").html(new Date(json.publishedTime).toLocaleDateString())
            for(var i=0;i<json.comments.length;i++){
                var html=`<h4>评论者:${json.comments[i].username}</h4><h5>评论内容:${json.comments[i].comment}</h5>
                        <h6>评论时间:${new Date(json.comments[i].leaveTime).toLocaleDateString()}</h6>===============
                            `
                $("#comments").append(html)
            }
        }
    })
    //返回主页按钮
    $("#mainPage").click(function (){
        window.location='blog_list.html?username=' + username + '&page=' + 1+"&time="+new Date().getTime()
    })
    //进入编辑页按钮
    $("#editBlog").click(function (){
        window.location='blog_edit.html?username=' + username + '&page=' + 1
    })
    $("#square").click(function (){
        window.location='blog_square.html?username='+username
    })
    //点击发送评论触发事件
    $("#send").click(function (){
        var author=username
        var articleId=id
        var commentText=$("#commentText").val()
        $.ajax({
            url: "/blogSystem/sendComment",
            data: {
                "author":author,
                "articleId":id,
                "commentText":commentText,
                "dateTime":new Date()
            },
            dataType: "text",
            success:function (text){
                alert(text)
                window.location="blog_specifics.html?articleId="+id+"&username="+username
            },
            error:function (){
                alert("发送失败")
            }
        })
    })

})